<?php

/**
 * This is the model class for table "bln_authitem".
 *
 * The followings are the available columns in table 'bln_authitem':
 * @property integer $_id
 * @property string $authname
 * @property integer $authtype_id
 * @property string $description
 * @property string $bizrule
 * @property string $authdata
 */
class BAuthItem extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return BAuthItem the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'bln_authitem';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('authname, authtype_id', 'required'),
            array('authtype_id', 'numerical', 'integerOnly' => true),
            array('authname', 'length', 'max' => 128),
            array('description, bizrule, authdata', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('_id, authname, authtype_id, description, bizrule, authdata', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'AuthType' => array(self::HAS_ONE, 'BAuthItemType', 'authtype_id'),
            //'subOperations' => array(self::HAS_MANY, 'BOperation', 'operation_id'),
            //'subFeatures' => array(self::HAS_MANY, 'BFeature', 'feature_id')
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            '_id' => 'ID',
            'authname' => 'Authname',
            'authtype_id' => 'Authtype',
            'description' => 'Description',
            'bizrule' => 'Bizrule',
            'authdata' => 'Authdata',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('_id', $this->_id);
        $criteria->compare('authname', $this->authname, true);
        $criteria->compare('authtype_id', $this->authtype_id);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('bizrule', $this->bizrule, true);
        $criteria->compare('authdata', $this->authdata, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function getChildren() {
        return $this->findChildrenById($this->_id);
    }

    public function getRecursivedChildren() {
        $children = array();
        $this->findRecursivedChildren($this, $children);

        if (empty($children))
            return false;

        return $children;

       
    }

    protected static function findChildrenById($authItemId) {
        $rows = $this->db->createCommand()
                        ->select()
                        ->from($this->authiItemChildTable)
                        ->where('parent_id=:parentid', array(':parentid' => $authItemId))
                        ->queryAll();
        $children = array();
        if ($rows->rowCount <= 0) {
            return false;
        } else {
            foreach ($rows as $row) {
                $children[$row['child_id']] = BAuthItem::model()->findByPk($row['child_id']);
            }
            return $children;
        }
    }

    public static function findRecursiveChildren($authItem, $authItems) {
        $children = array();
        while ($children = $authItem->Children) {
            array_merge($authItems, $children);
            foreach ($children as $authItem) {
                $this->findRecursiveChildren($authItem, $authItems);
            }
        }
    }

    public function getRecursiveOperations() {
        return BAuthItem::findRecursiveOperations($this);
    }

    public function getRecursiveFeatures() {
        return BAuthItem::findRecursiveFeatures($this);
    }

    public static function findRecursiveFeatures($authItem) {
        $features = BAuthItem::findSubFeaturesRecursiveChildren($authItem);

        $authItems = array();

        BAuthItem::findRecursivedChildren($authItem, $authItems);

        foreach ($authItems as $authItem) {
            array_merge($features, $authItem->subFeatures);
            $authFeatures = BAuthItem::findSubFeaturesRecursiveChildren($authItem);
            array_merge($features, $authFeatures);
        }
        if (empty($features) && $features == false)
            return false;

        return $features;
    }

    public static function findRecursiveOperations($authItem) {
        $operations = array();
        $operations = BAuthItem::findSubOperations($authItem);

        $authItems = array();
        BAuthItem::findRecursivedChildren($authItem, $authItems);

        foreach ($authItems as $authItem) {
            $subOperations = BAuthItem::findSubOperations($authItem);
            array_merge($operations, $subOperations);

            $features = BAuthItem::findRecursiveFeatures($authItem);

            foreach ($features as $feature) {
                array_merge($operations, $feature->operations);
            }
        }

        if (empty($operations))
            return false;

        return $operations;
    }

    private static function findSubFeaturesRecursiveChildren($authItem) {
        $features = BAuthItem::findSubFeatures($authItem);

        foreach ($features as $feature) {
            $featureChildren = array();
            BFeature::findRecursiveChildren($feature, $featureChildren);
            array_merge($features, $featureChildren);
        }
        if (empty($features))
            return false;

        return $features;
    }

    public static function findSubFeatures($authItem) {
        $subFeatures = array();

        $rows = $this->db->createCommand()
                        ->select()
                        ->from($this->authItemFeatureTable)
                        ->where('authitem_id=:authItemId', array(':authItemId' => $authItem))
                        ->queryAll();

        if ($rows->rowCount <= 0) {
            return false;
        } else {
            foreach ($rows as $row) {
                $subFeatures[$row['feature_id']] = BFeature::model()->findByPk($row['feature_id']);
            }
        }
        return $subFeatures;
    }

    public static function findSubOperations($authItem)
    {
         $subOperations = array();

        $rows = $this->db->createCommand()
                        ->select()
                        ->from($this->authItemOperationTable)
                        ->where('authitem_id=:authItemId', array(':authItemId' => $authItem))
                        ->queryAll();

        if ($rows->rowCount <= 0) {
            return false;
        } else {
            foreach ($rows as $row) {
                $subOperations[$row['operation_id']] = BFeature::model()->findByPk($row['operation_id']);
            }
        }
        return $subOperations;
    }
}